from flask import Blueprint, request

from py.PojosAndDaos.Account import *
from py.Services.AccountService import AccountService
from py.Services.BookService import BookService
from py.Services.LendService import LendService
from py.commonResponse.CommonResponse import *
from py.commonResponse.Dict import Dict

bp_lend = Blueprint('lend', __name__, url_prefix="/lends")  # 蓝图使用方法，参数里给定文件名，还可以给定url前缀


# 借书功能
@bp_lend.route('<int:amount>', methods=['POST'])
def lendBooks(amount):
    data = request.json
    account = AccountService.Account_Query_ByReaderID(data["ReaderID"])
    if account is None:
        return createForErrorWM('ReaderID不存在')
    book = BookService.Book_Query_ByISBN(data["ISBN"])
    if book == '通过ISBN没查到这本书':
        return createForErrorWM(book)
    msg = LendService.LendBooks(account, book, amount)
    return createForSuccessWM(msg)


# 还书功能
@bp_lend.route('', methods=['POST'])
def returnBook():
    record = request.json
    msg = LendService.ReturnBook(Dict(record), 1)
    return createForSuccessWM(msg)


# 申报书籍丢失
@bp_lend.route('/lostBook', methods=['POST'])
def reportLossOfBooks():
    record = request.json
    msg = LendService.ReportLossOfBooks(Dict(record))
    return createForSuccessWM(msg)

